//
//  ApplyShopVC.m
//  TerritoryShopkeeper
//
//  Created by 黄德玉 on 2018/6/3.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "ApplyShopVC.h"
#import "ApplyShopView.h"
#import "DYPhotoPicker.h"
#import "DYCommonWebView.h"

@interface ApplyShopVC ()

@property (nonatomic,strong) ApplyShopView * shopView;
@property (nonatomic,strong) DYPhotoPicker * photoPicker;

@property (nonatomic,strong) NSString * frontURL;
@property (nonatomic,strong) NSString * backURL;

@end

@implementation ApplyShopVC

- (void)loadView{
    self.view = self.shopView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"开店申请";
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (ApplyShopView *)shopView{
    if (!_shopView) {
        _shopView = [[ApplyShopView alloc] init];
        __weak typeof(self) weakSelf = self;
        _shopView.didClickedIndex = ^(NSInteger index) {
            //点击页面上的按钮
            if (index == 0) { //正面
                [weakSelf showPickerWithCallback:^(UIImage *img) {
                    [weakSelf.shopView updateFrontImg:img];
                    [weakSelf uploadImage:img success:^(NSString *imageUrl) {
                        weakSelf.frontURL = imageUrl;
                    }];
                }];
                return;
            }
            if (index == 1) { //反面
                [weakSelf showPickerWithCallback:^(UIImage *img) {
                    [weakSelf.shopView updateBackImg:img];
                    [weakSelf uploadImage:img success:^(NSString *imageUrl) {
                        weakSelf.backURL = imageUrl;
                    }];
                }];
                return;
            }
            if (index == 3) {
                [weakSelf showXieyi];
            }
            if (index == 4) {
                [weakSelf submit];
            }
        };
    }
    return _shopView;
}

- (void)showPickerWithCallback:(void (^)(UIImage * img))imgblock{
    UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"选取照片" preferredStyle:UIAlertControllerStyleActionSheet];
    __weak typeof(self) weakSelf = self;
    UIAlertAction * photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf.photoPicker showOnPickerViewControllerOnViewController:weakSelf completion:^(UIImage *image) {
            if (image && imgblock) {
                imgblock(image);
            }
        }];
    }];
    UIAlertAction * cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf.photoPicker showOnPickerViewControllerSourceType:UIImagePickerControllerSourceTypeCamera onViewController:weakSelf allowsEditing:NO compled:^(UIImage *image, NSDictionary *editingInfo) {
            if (image && imgblock) {
                imgblock(image);
            }
        }];
    }];
    [alertVC addAction:photoAction];
    [alertVC addAction:cameraAction];
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}



- (DYPhotoPicker *)photoPicker{
    if (!_photoPicker) {
        _photoPicker = [[DYPhotoPicker alloc] init];
    }
    return _photoPicker;
}

- (void)showXieyi{
    DYCommonWebView * wv = [[DYCommonWebView alloc] init];
    [self.navigationController pushViewController:wv animated:YES];
}

- (void)submit{
    if (!self.frontURL) {
        return;
    }
    if (!self.backURL) {
        return;
    }
    id pars = @{
                    @"uid":[UserInfo shareInstance].user.mid,
                    @"oname":self.shopView.name,
                    @"icardno":self.shopView.cardNum,
                    @"address":self.shopView.address,
                    @"icardfront":self.frontURL,
                    @"icardback":self.backURL,
                };
    __weak typeof(self) weakSelf = self;
    [MYAFNetworking POST:@"api2/openshopfor" parameters:pars success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        NSString * code = [responseObject safeObjectForKey:@"code"];
        if ([code isEqualToString:@"200"]) {
             [MBProgressHUD showError:weakSelf.view title:@"提交成功" code:code animated:YES];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popToRootViewControllerAnimated:YES];
            });
        }else{
            [MBProgressHUD showError:weakSelf.view title:[responseObject safeObjectForKey:@"message"] code:code animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
    }];
}





@end
